<template>
  <div class="app">
    <el-container>
      <el-header>
        <div class="nav">
          <div class="nav_title">
            <img src="	http://admin.erp.byesame.com/img/logo.f558a495.svg" alt="" />
            <span>进销存</span>
          </div>
          <div class="nav_other">
            <div class="fold hov">
              <span><i class="el-icon-s-fold" @click="menuchange()"></i></span>
            </div>
            <div class="others">
              <div class="hov">
                <span><i class="el-icon-search"></i></span>
              </div>
              <div class="hov">
                <span><i class="el-icon-question"></i></span>
              </div>
              <div class="hov">
                <span><i class="el-icon-bell"></i></span>
              </div>
              <div class="hov1">
                <span>
                  <span></span>
                  <span>欢迎您，管理员</span>
                </span>
              </div>
              <div class="hov1">
                <span>
                  <i class="el-icon-eleme"></i>
                  <span>退出登录</span>
                </span>
              </div>
            </div>
          </div>
        </div>
      </el-header>
      <el-container>
        <el-aside :width="`${wid}px`">
          <el-menu default-active="2" class="el-menu-vertical-demo" :collapse="isCollapse" background-color="#fff"
            text-color="#000" active-text-color="#ffd04b" router>
            <el-submenu @click.native="handleClick(item)" v-for="item in menudata" :key="item.id" :to="item.path"
              :index="item.path" class="no-arrow">
              <template slot="title">
                <i class="el-icon-setting"></i>
                <span :class="{ span: !item.children }"></span>
                <span>{{ item.meta.title }}</span>
              </template>
              <el-submenu :index="ite.path" @click.native="handleClick(ite)" v-for="ite in item.children" :key="ite.id">
                <template slot="title">{{ ite.meta.title }}
                  <span :class="{ span1: !ite.children }"></span>
                </template>
                <el-menu-item :index="it.path" v-for="it in ite.children" :key="it.id">{{ it.meta.title }}</el-menu-item>
              </el-submenu>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
    
<script>
import { Navigation } from "@/api/dashboard";
export default {
  created() {
    this.navigation();
  },
  data() {
    return {
      isCollapse: false,
      menudata: [],
      wid: 200,
    };
  },
  methods: {
    menuchange() {
      this.isCollapse = !this.isCollapse;
      if (!this.isCollapse) {
        this.wid = 200;
      } else {
        this.wid = 64;
      }
    },
    async navigation() {
      var shijian = Math.floor(Date.now() / 1000);
      const res = await Navigation(shijian, sessionStorage.getItem("token"));
      console.log(res);
      this.menudata = res.result.menu;
      // JSON.stringify(res.result.menu)
      // console.log(JSON.stringify(res.result.menu))
    },
    handleClick(item) {
      if (!item.children || item.children.length === 0) {
        if (this.$router.currentRoute.path !== item.path) {
          this.$router.push(item.path);
        }
      }
    },
  },
  computed: {
    parentItems() {
      return this.menudata.filter(
        (item) => item.children && item.children.length
      );
    },
    singleItems() {
      return this.menudata.filter(
        (item) => !item.children || !item.children.length
      );
    },
  },
};
</script>
    
<style>
.fold {
  font-size: 24px;
}

.others {
  display: flex;
  gap: 20px;
  margin-right: 30px;
}

.nav_title {
  color: #fff;
  font-size: 20px;
  text-align: left;
  font-weight: bold;
  line-height: 60px;
  margin-left: 30px;
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
}

.nav_other {
  display: flex;
  color: #fff;
  align-items: center;
  gap: 20px;
  justify-content: space-between;
  flex: 9;
}

.nav {
  display: flex;
  background-color: #389af9;
  justify-content: space-around;
  /* position: fixed; */
  width: 100%;
  /* z-index: 999; */
  justify-content: space-between;
}

.el-header {
  padding: 0;
}

.img,
img {
  width: 34px;
  height: 34px;
  cursor: pointer;
}

.circle {
  border-radius: 50%;
}

.hov {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.hov1 {
  width: 120px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.hov:hover,
.hov1:hover {
  background-color: #5eb2ff;
}

.el-aside {
  overflow-y: auto;
  /* 当侧边栏内容超出时显示滚动条 */
  height: 100vh;
  /* 设置高度为视口高度，防止超出 */
}

.el-main {
  /* overflow-y: auto; */
  /* 当主内容超出时显示滚动条 */
  /* height: calc(100vh - 60px); */
  padding: 0;
  background-color: #f0f2f5;
  /* 假设头部高度为60px */
}

.app {
  overflow: hidden;
  /* 阻止应用程序级别的滚动 */
}

.span,
.span1 {
  background: #fff;
  /* background: red; */
  width: 30px;
  height: 20px;
  position: absolute;
  left: 99%;
  top: 20px;
  transition: all 0.3s;
  transform: translateX(-30px);
  z-index: 5;
}

.el-submenu:hover .span {
  background: #cccccc;
}

.el-submenu .span1 {
  background: #fff;
}

.el-submenu .el-submenu:hover .span1 {
  background: #cccccc;
}
</style>